/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 *
 * @author Jonathan
 */
public class ManejadorDB {
    
    public static void ejecutarSQL(String sql) throws Exception
    {
        Class.forName(DataBaseReference.JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DataBaseReference.DB_NAME);
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.setAutoCommit(true);
        conn.close();
    }
    
    public static String getResultsFromColumns(String sql,int columns,
            String columnName1,String columnName2,String columnName3,String columnName4,String columnName5,String columnName6) throws Exception
    {
        Class.forName(DataBaseReference.JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DataBaseReference.DB_NAME);
        Statement stat = conn.createStatement();
        String result = "";
        
        ResultSet rs = stat.executeQuery(sql);
        
        while (rs.next()) {
            if(result!="")
            {
                result += result + ":";
            }
            switch(columns)
            {
                case 1:
                    result = result +" "+ rs.getString(columnName1);
                break;
                case 2:
                    result = result +" "+ rs.getString(columnName1)+" "+ rs.getString(columnName2);
                break;
                case 3:
                    result = result +" "+ rs.getString(columnName1)+" "+ rs.getString(columnName2)+" "+ rs.getString(columnName3);
                break;
                case 4:
                    result = result +" "+ rs.getString(columnName1)+" "+ rs.getString(columnName2)+" "+ rs.getString(columnName3)+" "+ rs.getString(columnName4);
                break;
                case 5:
                    result = result +" "+ rs.getString(columnName1)+" "+ rs.getString(columnName2)+" "+ rs.getString(columnName3)+" "+ rs.getString(columnName4)+" "+ rs.getString(columnName5);
                break;
                case 6:
                    result = result +" "+ rs.getString(columnName1)+" "+ rs.getString(columnName2)+" "+ rs.getString(columnName3)+" "+ rs.getString(columnName4)+" "+ rs.getString(columnName5)+" "+ rs.getString(columnName6);
                break;                
            }
        }
        conn.setAutoCommit(true);
        conn.close();
        
        return result;
    }
    
}
